<template>
  <div
    :class="[
      `pagination-container-${position}`,
      !sidebar.opened ? 'pagination-width' : '',
      {'hidden':hidden}
    ]"
  >
    <el-pagination
      hide-on-single-page
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :layout="layoutDevice"
      :page-sizes="pageSizes"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { scrollTo } from '@/utils/scroll-to'
import { mapGetters } from 'vuex'
export default {
  name: 'Cpagination',
  props: {
    total: {
      required: true,
      type: Number
    },
    page: {
      type: Number,
      default: 1
    },
    limit: {
      type: Number,
      default: 10
    },
    pageSizes: {
      type: Array,
      default() {
        return [10, 20, 30, 50]
      }
    },
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },
    background: {
      type: Boolean,
      default: true
    },
    autoScroll: {
      type: Boolean,
      default: true
    },
    hidden: {
      type: Boolean,
      default: false
    },
    // 自定义样式（可选值inline, fixed）
    position: {
      type: String,
      default: 'inline'
    }
  },
  computed: {
    ...mapGetters(['sidebar', 'device']),
    currentPage: {
      get() {
        return this.page
      },
      set(val) {
        this.$emit('update:page', val)
      }
    },
    pageSize: {
      get() {
        return this.limit
      },
      set(val) {
        this.$emit('update:limit', val)
      }
    },
    // 兼容移动端
    layoutDevice() {
      return this.device !== 'mobile' ? this.layout : 'prev, pager, next'
    }
  },
  methods: {
    handleSizeChange(val) {
      this.$emit('pagination', { page: this.currentPage, limit: val })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    handleCurrentChange(val) {
      this.$emit('pagination', { page: val, limit: this.pageSize })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";
@import "~@/styles/variables.scss";
@import "~@/styles/element-variables.scss";
.pagination-width{
  width: calc(100% - 54px) !important;
}
.pagination-container-inline{
  background: transparent;
  padding: 10px 0;
  text-align: center;
}
.pagination-container-fixed{
  border-top: 1px solid #DCDFE6;
  background: #F2F6FC;
  padding: 10px 0;
  text-align: center;
  position: fixed;
  bottom: 0;
  right: 0;
  z-index: 9;
  width: calc(100% - #{$sideBarWidth});
  transition: width 0.28s;
}
.pagination-container.hidden {
  display: none;
}
::v-deep .el-pagination.is-background .el-pager li:not(.disabled).active {
  background-color: #ECF5FF;
  color: $--color-primary;
  border: 1px solid $--color-primary;
}
::v-deep .el-pagination.is-background .el-pager li{
  border-radius: 5px;
}
</style>

